const path = require("path");
const resolve = dir => {
  return path.join(__dirname, dir);
};
const IS_PROD = process.env.NODE_ENV === "production";
// eslint-disable-next-line no-unused-vars
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer")
  .BundleAnalyzerPlugin;
const CompressionWebpackPlugin = require("compression-webpack-plugin");
const productionGzipExtensions = /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i;
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
module.exports = {
  pluginOptions: {
    "style-resources-loader": {
      preProcessor: "stylus",
      patterns: [resolve("src/common/stylus/*.styl")]
    }
  },
  // 基本路径
  publicPath: "./",

  outputDir: "dist",

  // eslint-loader 是否在保存的时候检查
  lintOnSave: true,

  runtimeCompiler: true,
  productionSourceMap: false,

  chainWebpack: config => {
    config.entry.app = ["./src/main.js"];
    if (IS_PROD) {
      config.plugins.delete("preload");
      config.plugins.delete("prefetch");
      config.optimization.minimize(true);
      // 分割代码
      config.optimization.splitChunks({
        chunks: "all",
        cacheGroups: {
          libs: {
            name: "chunk-libs",
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: "initial"
          },
          commons: {
            name: "chunk-commons",
            test: resolve("src/components"),
            minChunks: 2,
            priority: 5,
            reuseExistingChunk: true
          }
        }
      });
      config.plugin("html").tap(args => {
        args[0].chunksSortMode = "none";
        return args;
      });
    }

    // 修复HMR
    config.resolve.symlinks(true);
    //修复 Lazy loading routes Error
    // 添加别名
    config.resolve.alias
      .set("@", resolve("src"))
      .set("util", resolve("src/utils"))
      .set("common", resolve("src/common"))
      .set("_c", resolve("src/components"));
  },

  configureWebpack: config => {
    if (IS_PROD) {
      const plugins = [];
      //开启 gzip 压缩
      plugins.push(
        new CompressionWebpackPlugin({
          filename: "[path].gz[query]",
          algorithm: "gzip",
          test: productionGzipExtensions,
          threshold: 10240,
          minRatio: 0.8
        }),
        new UglifyJsPlugin({
          uglifyOptions: {
            compress: {
              drop_console: true, //console
              drop_debugger: false,
              pure_funcs: ["console.log"] //移除console
            }
          },
          sourceMap: false,
          parallel: true
        })
        /*new BundleAnalyzerPlugin()*/
      );
      config.plugins = [...config.plugins, ...plugins];
    }
  },

  // css相关配置
  css: {
    extract: true,
    sourceMap: false,
    loaderOptions: {
      postcss: {
        plugins: [
          require("postcss-pxtorem")({
            rootValue: 37.5,
            propList: ["*"]
          })
        ]
      }
    },
    modules: false
  },

  parallel: require("os").cpus().length > 1,

  devServer: {
    host: "0.0.0.0",
    port: 8080,
    https: false,
    hotOnly: true,
    hot: true,
    compress: false, // 开启压缩
    overlay: {
      warnings: false,
      errors: true
    },
    disableHostCheck: true,
    proxy: {
      "/api": {
        target: "http://47.95.210.86:3000",
        changeOrigin: true,
        crossOrigin: true,
        withCredentials: true
      }
    }
  }
};
